package com.wzx.ptplatform.controller.admin.user;

import com.wzx.ptplatform.domain.*;
import com.wzx.ptplatform.service.CompanyService;
import com.wzx.ptplatform.service.JobService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员用户模块——企业信息管理")
@RestController
public class AdminUserCompanyController {

    final CompanyService companyService;
    final JobService jobService;

    public AdminUserCompanyController(CompanyService companyService, JobService jobService) {
        this.companyService = companyService;
        this.jobService = jobService;
    }

    @RequestMapping("admin_user_company_message_list")
    public LayuiPager<Company> adminUserCompanyMessageList(
            String companyName, String industryName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Company company = new Company();
        if (companyName != null && !"".equals(companyName)) {
            company.setCompanyName(companyName);
        }
        if (industryName != null && !ConstUtils.PLEASE_CHOOSE.equals(industryName)) {
            company.setIndustry(new Industry(Long.parseLong(industryName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            company.setCity(new City(Long.parseLong(cityName)));
        }
        Page<Company> pager = companyService.findList(company, page - 1, limit);
        LayuiPager<Company> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_user_company_message_addView")
    public ModelAndView adminUserCompanyMessageAddView() {
        return new ModelAndView("admin/user/company_message_add");
    }

    @GetMapping("admin_user_company_message_editView")
    public ModelAndView adminUserCompanyMessageEditView(String id, Model model) {
        model.addAttribute("company", companyService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/user/company_message_edit");
    }

    @PostMapping("admin_user_company_message_save_or_edit")
    public Map<String, Object> adminUserCompanyMessageSaveOrEdit(Company company) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (companyService.saveOrEdit(company)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_company_message_delete")
    public Map<String, Object> adminUserCompanyMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (companyService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_company_message_delete_select")
    public Map<String, Object> adminUserCompanyMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    companyService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
